﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace CarBloke.Pages
{
    public partial class VehicleLogPage : PhoneApplicationPage
    {
        Vehicle _vehicle = null;

        public VehicleLogPage()
        {
            InitializeComponent();

            //Application bar and application menu text must be set here explicitly.
            var newButton = ApplicationBar.Buttons[0] as ApplicationBarIconButton;
            newButton.Text = LanguageResources.Resources._new;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string vehicleID = "";
            if (NavigationContext.QueryString.TryGetValue("vehicleID", out vehicleID))
            {
                Guid id = new Guid(vehicleID);
                _vehicle = App.LoadVehicle(id);

                List<LogEntry> logEntries = App.LoadLogEntries(id);

                var fuelLogs = from log in logEntries
                               where log.LogType == LogEntry.LogTypes.Fuel
                               orderby log.LogTime
                               select log;

                var oilLogs = from log in logEntries
                              where log.LogType == LogEntry.LogTypes.Oil
                              orderby log.LogTime
                              select log;

                var maintenanceLogs = from log in logEntries
                                      where log.LogType == LogEntry.LogTypes.Other
                                      orderby log.LogTime
                                      select log;

                FuelLogEntryListBox.ItemsSource = fuelLogs.Reverse();
                OilLogEntryListBox.ItemsSource = oilLogs.Reverse();
                MaintenanceLogEntryListBox.ItemsSource = maintenanceLogs.Reverse();
            }
        }

        private void LogEntryListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;

            if (listBox != null)
            {
                // If selected index is -1 (no selection) do nothing
                if (listBox.SelectedIndex == -1)
                    return;

                LogEntry logEntry = listBox.SelectedItem as LogEntry;

                if (logEntry != null)
                {
                    NavigationService.Navigate(new Uri(PageRegistry.LogEditPage
                        + "?vehicleID=" + logEntry.VehicleID.ToString()
                        + "&logID=" + logEntry.ID.ToString(),
                        UriKind.RelativeOrAbsolute));
                }

                // Reset selected index to -1 (no selection)
                listBox.SelectedIndex = -1;
            }
        }

        private void appbar_newLogButton_Click(object sender, EventArgs e)
        {
            LogEntry.LogTypes logType = LogEntry.LogTypes.Fuel;
            if (PivotControl.SelectedItem == FuelPivotItem)
            {
                logType = LogEntry.LogTypes.Fuel;
            }
            else if (PivotControl.SelectedItem == OilPivotItem)
            {
                logType = LogEntry.LogTypes.Oil;
            }
            else if (PivotControl.SelectedItem == MaintenancePivotItem)
            {
                logType = LogEntry.LogTypes.Other;
            }

            NavigationService.Navigate(new Uri(PageRegistry.LogEditPage
                + "?vehicleID=" + _vehicle.ID.ToString()
                + "&logType=" + logType.ToString(),
                UriKind.RelativeOrAbsolute));
        }
    }
}